<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>单项链表</title>
</head>

<body>
    <script>
        //封装链表
        function LikeList() {
            //内部的类:节点类
            function Node(data) {
                this.data = data
                this.next = null
            }
            //属性
            this.head = null
            this.lenght = 0
            //1追加方法
            LikeList.prototype.append = function (data) {
                //1、创建新的节点
                var newnode = new Node(data)
                //2、判断是否是添加的是第一个节点
                if (this.lenght == 0) {//2.1是第一个节点
                    this.head = newnode
                } else {//2.2不是第一个节点
                    var current = this.head
                    while (current.next) {
                        current = current.next
                    }
                    //最后节点的next指向新的节点
                    current.next = newnode
                }
                this.lenght += 1;
            }
            //2toString方法
            LikeList.prototype.toString = function () {
                var current = this.head
                var listTring = ''
                while (current) {
                    listTring += current.data + ' '
                    current = current.next
                }
                return listTring
            }

            //3insert方法
            LikeList.prototype.insert = function (position, data) {
                if (position < 0 || position > this.lenght) return false
                //1、根据date创建newNode
                var newNode = new Node(data)
                //2、判断插入的位置是否是第一个
                if (position == 0) {
                    newNode.next = this.head
                    this.head = newNode
                } else {
                    var index = 0
                    var current = this.head
                    var previous = null
                    while (index++ < position) {
                        previous = current
                        current = current.next
                    }
                    newNode.next = current
                    previous.next = newNode
                }
                this.lenght += 1
                return true
            }
            //4get方法
            LikeList.prototype.get = function (position) {
                //月结
                if (position < 0 || position > this.lenght) return null;
                //获取对应的data
                var currrent = this.head
                var index = 0
                while (index++ < position) {
                    currrent = currrent.next
                }
                return currrent.data
            }

            //indexof方法
            LikeList.prototype.indexof = function (data) {
                var current = this.head
                var index = 0
                while (current) {
                    if (current.data == data) {
                        return index
                    }
                    current = current.next;
                    index += 1
                }
                return -1;
            }

            //update方法
            LikeList.prototype.update = function (position, newData) {
                //越界判断
                if (position < 0 || position >= this.lenght) return false;
                //查找正确节点
                var currrent = this.head
                var index = 0
                while (index++ < position) {
                    currrent = currrent.next
                }
                currrent.data = newData
                return true;
            }
            LikeList.prototype.removeAt = function (position) {
                //越界判断
                if (position < 0 || position >= this.lenght) return false;
                //判断是否删除的是第一个节点
                var currrent = this.head;
                if (position == 0) {
                    this.head = this.head
                } else {
                    var index = 0;
                    var previous = null;
                    while (index++ < position) {
                        previous = currrent
                        currrent = previous.next
                    }
                    previous.next = currrent.next
                }
                this.lenght -= 1
                return currrent.data
            }

            //remove方法
            LikeList.prototype.remove = function(data) {
                var position = this.indexof(data)
                return this.removeAt(position)
            }

            LikeList.prototype.isEmpty = function() {
                return this.lenght===0
            }

            LikeList.prototype.size = function() {
                return this.lenght
            }


        }
        var list = new LikeList()
        list.append('adv')
        list.append('asdasd')
        list.append('wewe')
        console.log('list', list)
        list.insert(0, 'adv')
        list.insert(3, '888')
        list.insert(5, '555')
        console.log('insert', list)
        console.log('get', list.get(0))
        console.log('get', list.get(3))
        console.log('get', list.get(5))
        console.log('indexof', list.indexof('888'))
        console.log('indexof', list.indexof('555'))
        list.update(0, '你好')
        list.update(3, 'haha')
        console.log('update', list)
        list.removeAt(0)
        console.log('removeAt', list)
        list.removeAt(3)
        console.log('removeAt', list)
        list.remove('你好')
        console.log('remove', list)
        console.log('list.isEmpty()',list.isEmpty(),list.size())
    </script>
</body>

</html>